package com.ruoyi.web.controller.example;

import com.alibaba.fastjson2.JSON;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.exception.BizException;
import com.ruoyi.resShop.domain.NewResShop;
import com.ruoyi.resShop.service.ResShopService;
import com.ruoyi.resturant.domain.ResShop;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.concurrent.TimeUnit;

@Anonymous
@Api(tags = "店铺管理")
@RestController
public class NewResShopController {

    @Autowired
    ResShopService service;

    @Autowired
    RedisCache redis ;
    //动态查询，if
    @PostMapping ("/getShopByIf")
    @ApiOperation(value = "获取店铺信息")
    public AjaxResult getShopByIf(@RequestBody NewResShop re){
        //先判断id是否存在，如果不存在在报异常
        //判断店铺id是否存在，最简单的方法是写一个根据id的查询，如果每查到数据
        //说明不存在，如果查到了，说明存在
        if (re.getId() <=0){
            //说明参数错误
            throw new BizException(500,"id不能为空");
        }
        NewResShop result = service.getShopById(re.getId());
        if (null == result){
            //说明参数错误
            throw new BizException(500,"该店铺id不存在");
        }
        //需要注意，这的id传入进来，其实不影响查询结果
        //因为我们的xml里，没有配置这个条件

        return AjaxResult.success(service.selectShopByIf(re));
    }


    //动态查询，when
    @PostMapping ("/getShopByWhen")
    @ApiOperation(value = "获取店铺信息-when")
    public AjaxResult getShopByWhen(@RequestBody NewResShop re){
        return AjaxResult.success(service.selectShopByWhen(re));
    }

    //新增店铺
    @PostMapping("/InsertNewResShop")
    @ApiOperation(value = "小董的店铺")
    public AjaxResult InsertNewResShop(@RequestBody NewResShop newResShop){
        return AjaxResult.success(service.insertNewResShop(newResShop));
    }

    //修改店铺 董虹妤2619
    @PostMapping("/UpdateNewResShop")
    @ApiOperation(value = "修改店铺")
    public AjaxResult UpdateStudent(@RequestBody NewResShop newResShop){
        return AjaxResult.success(service.updateNewResShop(newResShop));
    }

    //删除学生 //董虹妤2619
    @PostMapping("/DeleteNewResShop")
    @ApiOperation(value = "删除店铺")
    public AjaxResult DeleteStudent(int id){
        return AjaxResult.success(service.deleteNewResShop(id));
    }

    //redis测试   董虹妤2619
    @GetMapping("/testRedis")
    @ApiOperation(value = "测试redis")
    public AjaxResult testRedis(String name){
        //想使用，就得先获取到redis对象
        redis.setCacheObject("myName",name,20,TimeUnit.SECONDS);
        return AjaxResult.success("姓名存入redis成功");
    } //体会redis缓存作用
    @GetMapping("/GetShopByName")
    @ApiOperation(value = "redis作为缓存")
    public AjaxResult GetShopByName(String name){
        //先查询redis，看看有没有数据，有数据直接返回，就不走下面代码了
        //1w人同时查询，只有一次查了数据库，9999次都查了redis
        String result =  redis.getCacheObject(name);
        if( result != "" && result != null){
            return AjaxResult.success("我是直接从redis获取的："+result);
        }
        //如果上面没有返回，说明redis中没有查询到，那么就需要去mysql中再次查询
        //生成一个resshop对象，并赋值
        NewResShop re = new NewResShop();
        re.setName(name);
        List<NewResShop> list = service .selectShopByIf(re);
        String json = JSON.toJSONString(list);
        redis.setCacheObject(name,json);//这一步把mysql获取的数据存入到了redis里
        return AjaxResult.success(service.selectShopByIf(re));
    }



    //查询所有店铺接口

    @ApiOperation(value = "查询所有店铺接口")
    @Log(title = "获取所有店铺",businessType = BusinessType.OTHER)
    @GetMapping("/GetShopList")
    public AjaxResult GetShopList(){
       return AjaxResult.success(service.getShopList());
    }
}

